using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.Collections.Generic;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ResourceFolderTree : AdminBaseUserControl
    {
        public event EventHandler TreeNodeClick;
        //public event EventHandler EvenLogMenu;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //if (EvenLogMenu != null)
                //    EvenLogMenu(this, new EventArgs());
                DataBind();                 
            }

            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvFolder.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {
                            if (SelectedValue != null && tvFolder.SelectedNode.Depth == 1)
                            {
                                this.SelectedValue = tvFolder.SelectedValue;

                                TreeNodeClick(this, e);
                            }
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }

        }

        public string SelectedValue
        {
            get { return Convert.ToString(ViewState["_ClassID"]); }
            set { ViewState["_ClassID"] = value; }
        }

        private void PopulateBind(IResourceFolder resourceFolder, TreeNode node)
        {
            if (resourceFolder.SubFolderList!= null)
                foreach (IResourceFolder reFolder in resourceFolder.SubFolderList)
                {
                    TreeNode subNode = new TreeNode(reFolder.Name, reFolder.ID.ToString());
                    node.ChildNodes.Add(subNode);
                    PopulateBind(reFolder, subNode);
                }
        }

        public override void DataBind()
        {
            tvFolder.Nodes.Clear();            
            IResourceFolder root = ResourceFolderController.GetResourceFolderTree();
            TreeNode nodeRoot = new TreeNode("Folders", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBind(root, nodeRoot);
            tvFolder.Nodes.Add(nodeRoot);
            tvFolder.ExpandAll();
        }

        
        public void BindRefesh(object sender, EventArgs e)
        {
            DataBind();
        }


        protected void tvFolder_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TreeNodeClick != null)
            {
                this.SelectedValue = tvFolder.SelectedValue;

                TreeNodeClick(this, e);
            }
            ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
        }


        public void DoDeleteFolder(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectedValue.ToString()))
            {
                long classID = Convert.ToInt64(this.SelectedValue);
                ResourceFolderController.DeleteResourceFolder(classID);   
                DataBind();
            }
        }


    }
}